<?php
// +----------------------------------------------------------------------
//个人定制Ajax分页
namespace Think;

class AjaxPage{
	  //每页显示数量
	  public $perPage=5;
	  //当前页码默认值
	  public $curPage=1;
	  //总页码数
	  public $pageAll=1; 
	  //获得新闻纪录总数
	  public $Counts;
	  //初始化数据
	  public $pageOffset=1;
	  //当前分页显示的页码数
	  public $pageMax=3;
	  //第一个出现的分页页码
	  public $pageFirst=1;
	  
	  public function __construct($Counts,$curPage,$perPage)
	  {
	  	//获得页码总数，向上取整函数ceil 因为如果记录数除以每页显示数据数量有余数 有余数的话就要新开一个分页 向上加+1
	  	$this->pageAll=ceil($Counts/$perPage);
	  	//从哪个地方开始取数据
	    $this->pageOffset=($curPage-1)*$perPage;
	    $this->curPage=$curPage;
	    $this->Counts=$Counts;
	    $this->perPage=$perPage;
	  }
	  public function getCounts()
	  {
		  if($this->pageMax >$this->pageAll)
		  {
			  if($pageAll%2==0)
			  {
				  $this->pageMax=$this->pageAll-1;
			  }
			  else
			  {
				  $this->pageMax=$this->pageAll-2;
			  }
		  }
		  //距离中间有几个页码数注意这个ceil算多一个了
		  $pageConent=ceil($this->pageMax/2);
		  if($this->curPage<=$pageConent)
		  {
			  $this->pageFirst=1;
		  }
		  else if($this->curPage > $this->pageAll-$pageConent+1)
		  {
			  $this->pageFirst=$this->pageAll-$this->pageMax+1;
		  }
		  else
		  {
			  $this->pageFirst=$this->curPage-$pageConent+1;
		  }
		  return array(
           'pageFirst'=>$this->pageFirst,
           'pageAll'=>$this->pageAll,
		   'pageMax'=>$this->pageMax,
		   'pageOffset'=>$this->pageOffset,
		   'curPage'=>$this->curPage,
          );
      }
     
}
